<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '轮播图添加' : '轮播图修改'"
      :visible.sync="info.isshow"
      width="50%"
    >
     <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- 图片 -->
        <el-form-item label="图片" label-width="80px">
          <!-- 上传文件 -->
          <div class="updata">
            <p class="jia">+</p>
            <input type="file" class="ipt" @change="changefile" />
            <img v-if="imgUrl" :src="imgUrl" alt="" />
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="Add1" v-if="info.isAdd">添加</el-button>
      <el-button type="primary" @click="xiugai" v-else>修改</el-button>
      <el-button type="info" @click="quxiao">取消</el-button>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqbanneradd,reqbannerinfo,reqbanneredit } from "../../../http/api";
import { successAlert,errorAlert } from "../../../utils/alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      imgUrl: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      list: "banner/list",
    }),
  },
  mounted() {
    this.reqList();
  },
  methods: {
    ...mapActions({
      reqList: "banner/reqList",
    }),
    changefile(e) {
      let file = e.target.files[0];
      let imgArr=['.jpg','.jpeg','.png','.gif'];
      let extname=path.extname(file.name)
      if(!imgArr.includes(extname)){
        errorAlert("请上传正确的图片格式")
        return;
      }
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    //点了添加
    Add1() {
      reqbanneradd(this.form).then((res)=>{
        if(res.data.code == 200){
          successAlert(res.data.msg);
          this.reqList()
          this.info.isshow=false;
          this.empty()
        }
      })
    },
    xiugai(){
      reqbanneredit(this.form).then((res)=>{
        if(res.data.code == 200){
          successAlert(res.data.msg);
          this.empty()
          this.info.isshow=false;
          this.reqList()
        }
      })
    },
    quxiao(){
      if(!this.info.isAdd){
        this.empty()
      }
      this.info.isshow = false;
    },
    //定义一个空数组
    empty() {
      // 清空form
      this.form = {
       title: "",
        img: null,
        status: 1,
      };

      // 清除图片地址 
      this.imgUrl="";
      
    },
    getOne(id){
      //取回数据并对其进行操作
      reqbannerinfo({id:id}).then((res)=>{
        if(res.data.code == 200){
          successAlert(res.data.msg)
          this.form = res.data.list;
          this.form.id = id;
          this.imgUrl=this.$pre+this.form.img
        }
      })
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.updata {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary;
  position: relative;
  overflow: hidden;
  border-radius: 20px;
}
.jia {
  width: 100%;
  height: 100%;
  font-size: 30px;
  line-height: 100px;
  text-align: center;
  color: @primary;
}
.ipt {
  width: 100px;
  height: 100px;
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 10;
}
img {
  width: 100px;
  height: 100px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
}
</style>